import * as apply from '../api/apply';
import { message } from 'antd/lib/index';
import * as company from '../api/company';
import checkStatus from '@/utils/status';

export default {
  namespace: 'equityModels',
  state: {},
  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(apply.applyList, payload);
      yield checkStatus(response);
      yield put({
        type: 'getList',
        payload: response.msg,
      });
    },
    * EnterpriseList({ payload }, { call, put }) {
      const response = yield call(company.EnterpriseList, payload);
      yield checkStatus(response);

      yield put({
        type: 'get',
        payload: response.msg,
      });
    },
    * adds({ payload, callback }, { call, put }) {
      const response = yield call(apply.addApply, payload);
      yield checkStatus(response);

      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback(response);
    },
    * remove({ payload, callback }, { call, put }) {
      const response = yield call(apply.applyRemove, payload);
      yield checkStatus(response);

      yield put({
        type: 'applyRemove',
        payload: response,
      });
      if (callback) callback(response);
    },
    * update({ payload, callback }, { call, put }) {
      const response = yield call(apply.applyEdit, payload);
      yield checkStatus(response);

      if (callback) callback(response);
    },
    * getApplyDesc({ payload, callback }, { call, put }) {
      const response = yield call(apply.applyDetail, payload);
      yield checkStatus(response);

      yield put({
        type: 'getDesc',
        payload: response.msg,
      });
      if (callback) callback();
    },

    * getUserList({ payload }, { call, put }) {
      const response = yield call(UserList, payload);
      yield checkStatus(response);

      yield put({
        type: 'userList',
        payload: response.msg,
      });
    },

    * deptSort({ payload }, { call, put }) {
      const response = yield call(sortChange, payload);
      yield checkStatus(response);

      yield put({
        type: 'sortChangeReduce',
        payload: response.msg,
      });
    },
    * audit({ payload, callback }, { call }) {
      const response = yield call(apply.applyAudit, payload);
      yield checkStatus(response);

      if (callback) callback(response);
    },
    * generator({ payload, callback }, { call }) {
      const response = yield call(apply.productGenerator, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * role({ payload, callback }, { call, put }) {
      const response = yield call(apply.applyRule, payload);
      yield checkStatus(response);
      yield put({
        type: 'roleList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    getList(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        data: action.payload,
      };
    },
    get(state, action) {
      return {
        ...state,
        companyList: action.payload,
      };
    },
    save(state, action) {

      return {
        ...state,
        data: action.payload,
      };
    },
    edit(state, action) {
      if (action.payload.code === 200) {
        message.success('更新成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },

    userList(state, action) {
      return {
        ...state,
        userList: action.payload.data,
      };
    },
    getDesc(state, action) {
      return {
        ...state,
        applyDesc: action.payload,
      };
    },
    roleList(state, action) {
      return {
        ...state,
        setRoleList: action.payload,
      };
    },
    sortChangeReduce(state, action) {
      return {
        ...state,
        data: action.payload,

      };
    },
  },
};
